package com.example.myapplication.image;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.myapplication.R;

public class TranslateView extends View {
    private int rotate;

    public TranslateView(Context context){
        super(context);
    }
    public TranslateView(Context context,@Nullable AttributeSet attrs){
        super(context,attrs);
        TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.TranslateView);
        final int N=a.getIndexCount();
        for(int i=0;i<N;++i){
            int attr=a.getIndex(i);
            switch (attr){
                case R.styleable.TranslateView_rotate:
                rotate=a.getInt(attr,0);
                break;
            }
        }
        a.recycle();
    }
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(),R.drawable.husky);
        Matrix matrix=new Matrix();
        float px=canvas.getWidth()/2;
        float py=canvas.getHeight()/2;
        matrix.postTranslate(-bitmap.getWidth()/2,-bitmap.getHeight()/2);
        matrix.postRotate(rotate);
        matrix.postTranslate(px,py);
        canvas.drawBitmap(bitmap,matrix,null);
    }
    public TranslateView(Context context, @Nullable AttributeSet attrs,int defStyleAttr){
        super(context,attrs,defStyleAttr);
    }
}



